package com.poposchool.project.service.impl;

import com.poposchool.project.common.Constants;
import com.poposchool.project.mapper.MenuMapper;
import com.poposchool.project.exception.ServiceException;
import com.poposchool.project.entity.Menu;
import com.poposchool.project.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Wang Jie
 * @Date 2022/7/5 10:38
 * @Version 1.0
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Resource
    MenuMapper menuMapper;


    @Override
    public Menu getMenuById(Integer id) {
        Menu menu;
        try {
            menu = menuMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return menu;
    }

    @Override
    public void deleteMenuById(Integer id) {
        try {
            menuMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void UpdateMenu(Menu menu) {
        try {
            menuMapper.updateByPrimaryKeySelective(menu);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public void insertMenu(Menu menu) {
        try {
            menuMapper.insertMenu(menu);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
    }

    @Override
    public Integer getAllNum() {
        Integer all;
        try {
            all = menuMapper.getAllNum();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return all;
    }

    @Override
    public List<Menu> getMenus() {
        List<Menu> menus;
        try {
            menus = menuMapper.findAll();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return menus;
    }

    @Override
    public List<Menu> getPageMenus(Integer pageNum, Integer pageSize, String name) {
        List<Menu> menus;
        try {
            menus = menuMapper.getPageMenus((pageNum - 1) * pageSize, pageSize, name);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return menus;
    }

    @Override
    public List<Menu> getMenusConstruction() {
        List<Menu> menus;
        List<Menu> fatherMenus;
        try {
            menus = getMenus();
            //获得父级元素
            fatherMenus = menus.stream().filter(menu -> menu.getPId() == null).collect(Collectors.toList());
            for (Menu menu : fatherMenus) {
                menu.setChildren(menus.stream().filter(m -> menu.getId().equals(m.getPId())).collect(Collectors.toList()));
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return fatherMenus;
    }
}
